import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.GrandExchange;
import org.rsbot.script.methods.GrandExchange.GEItem;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.util.Timer;
import org.rsbot.script.wrappers.RSArea;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.script.wrappers.RSTilePath;

@ScriptManifest(authors={"LastCoder"}, keywords={"Cooker, Auto"}, name="ArbiTannerLite", version=2.0D, description="Start, all options are in GUI.")
public class AutoTanner extends Script
  implements MessageListener, PaintListener
{
  private static final AutoTanner.Hide[] HIDE_ARRAY = { new AutoTanner.Hide("Cow Hide", 1739, 1741, 2), new AutoTanner.Hide("Green Dragon Hide", 1753, 1745, 5), new AutoTanner.Hide("Red Dragon Hide", 1749, 2507, 7), new AutoTanner.Hide("Blue Dragon Hide", 1751, 2505, 6), new AutoTanner.Hide("Black Dragon Hide", 1747, 2509, 8) };

  private static final Color COLOR_1 = new Color(0, 0, 0, 155);
  private static final Color COLOR_2 = new Color(0, 0, 0);
  private static final Color COLOR_3 = new Color(255, 255, 255);
  private static final BasicStroke STROKE = new BasicStroke(1.0F);
  private static final Font FONT_1 = new Font("Arial", 0, 17);
  private static final Font FONT_2 = new Font("Arial", 0, 9);
  private static final int TANNER = 2824;
  private static final RSTile[] OLD_PATH = { new RSTile(3269, 3166), new RSTile(3270, 3166), new RSTile(3271, 3166), new RSTile(3272, 3166), new RSTile(3273, 3166), new RSTile(3274, 3167), new RSTile(3274, 3167), new RSTile(3274, 3168), new RSTile(3274, 3170), new RSTile(3274, 3170), new RSTile(3275, 3171), new RSTile(3275, 3171), new RSTile(3275, 3172), new RSTile(3275, 3173), new RSTile(3275, 3173), new RSTile(3277, 3179), new RSTile(3278, 3180), new RSTile(3278, 3180), new RSTile(3279, 3181), new RSTile(3279, 3181), new RSTile(3280, 3182), new RSTile(3281, 3183), new RSTile(3281, 3183), new RSTile(3281, 3184), new RSTile(3281, 3185), new RSTile(3281, 3186), new RSTile(3281, 3186), new RSTile(3281, 3187), new RSTile(3281, 3190), new RSTile(3280, 3191), new RSTile(3278, 3191), new RSTile(3276, 3192) };
  private RSTilePath current_path;
  private RSTilePath path_back;
  private AutoTanner.Hide hide;
  private boolean gui_on;
  private int profit;
  private int profitMade;
  private int made;
  private long startTime;

  public AutoTanner()
  {
    this.gui_on = false;
  }

  public boolean atBank()
  {
    RSArea localRSArea = new RSArea(new RSTile(3264, 3159), new RSTile(3274, 3174));
    return localRSArea.contains(new RSTile[] { getMyPlayer().getLocation() });
  }

  public boolean atTanner() {
    RSArea localRSArea = new RSArea(new RSTile(3270, 3189), new RSTile(3277, 3195));
    return localRSArea.contains(new RSTile[] { getMyPlayer().getLocation() });
  }

  public AutoTanner.state getState() {
    RSInterface localRSInterface = this.interfaces.get(324);
    if (localRSInterface.isValid())
      return AutoTanner.state.INTERFACE;
    if ((this.walking.getEnergy() > random(80, 100)) && (!this.walking.isRunEnabled()))
    {
      return AutoTanner.state.TURN_ON_RUN;
    }if (atBank()) {
      if (this.inventory.contains(this.hide.ind_id)) {
        return AutoTanner.state.WALK_TO;
      }
      return AutoTanner.state.BANK;
    }
    if (atTanner()) {
      if (this.inventory.contains(this.hide.ind_id)) {
        return AutoTanner.state.TRADE_TANNER;
      }
      return AutoTanner.state.WALK_FROM;
    }

    if (this.inventory.contains(this.hide.ind_id)) {
      return AutoTanner.state.WALK_TO;
    }
    return AutoTanner.state.WALK_FROM;
  }

  public boolean onStart()
  {
    new AutoTanner.Gui(this).setVisible(true);
    while (this.gui_on) {
      sleep(20);
    }
    this.current_path = this.walking.newTilePath(OLD_PATH);
    this.path_back = this.walking.newTilePath(OLD_PATH).reverse();
    this.profit = (this.grandExchange.lookup(this.hide.tanned_id).getMaxPrice() - this.grandExchange.lookup(this.hide.ind_id).getMaxPrice());
    this.startTime = System.currentTimeMillis();
    return this.game.isLoggedIn();
  }

  public int loop()
  {
    switch (AutoTanner.1.$SwitchMap$AutoTanner$state[getState().ordinal()]) {
    case 1:
      this.walking.setRun(true);
      for (int i = 0; (i < 10) && (!this.walking.isRunEnabled()); i++)
        sleep(100, 200);
      sleep(random(1200, 1500));
      break;
    case 2:
      this.walking.rest();
      sleep(random(1200, 1500));
      break;
    case 3:
      this.current_path.traverse();
      break;
    case 4:
      this.path_back.traverse();
      break;
    case 5:
      RSNPC localRSNPC = this.npcs.getNearest(new int[] { 2824 });
      if ((localRSNPC == null) || (getMyPlayer().getAnimation() != -1)) break;
      localRSNPC.doAction("Trade");
      sleep(random(1200, 1500)); break;
    case 6:
      if (!this.bank.isOpen()) {
        if (getMyPlayer().getAnimation() != -1)
          sleep(random(1200, 1500));
        else
          this.bank.open();
      }
      else
      {
        if (this.inventory.contains(this.hide.ind_id)) break;
        if (this.inventory.contains(this.hide.tanned_id)) {
          this.bank.deposit(this.hide.tanned_id, 0);
          sleep(random(1200, 1500));
        }
        if (this.bank.getItem(this.hide.ind_id) != null) {
          this.bank.withdraw(this.hide.ind_id, 0);
          sleep(random(1200, 1500));
        } else {
          log("Out of Hides.");
        }
      }break;
    case 7:
      RSInterface localRSInterface = this.interfaces.get(324);
      if (!localRSInterface.isValid()) break;
      localRSInterface.getComponent(this.hide.component_id).doAction("Tan All");
      for (int j = 0; (j < 100) && (this.inventory.contains(this.hide.tanned_id)); j++) {
        sleep(20);
      }
      this.made += this.inventory.getCount(new int[] { this.hide.tanned_id });
      this.profitMade = (this.profit * this.made);
    }

    return random(800, 1200);
  }

  public void messageReceived(MessageEvent paramMessageEvent)
  {
  }

  public void onRepaint(Graphics paramGraphics)
  {
    Graphics2D localGraphics2D = (Graphics2D)paramGraphics;
    long l = System.currentTimeMillis() - this.startTime;
    String str = Timer.format(l);
    int i = (int)(3600 * this.profitMade / l);
    localGraphics2D.setColor(COLOR_1);
    localGraphics2D.fillRect(14, 350, 474, 99);
    localGraphics2D.setColor(COLOR_2);
    localGraphics2D.setStroke(STROKE);
    localGraphics2D.drawRect(14, 350, 474, 99);
    localGraphics2D.setFont(FONT_1);
    localGraphics2D.setColor(COLOR_3);
    localGraphics2D.drawString("AutoTanner", 209, 374);
    localGraphics2D.setFont(FONT_2);
    localGraphics2D.drawString("Profit: " + this.profitMade, 18, 390);
    localGraphics2D.drawString("Profit Hour: " + i, 18, 400);
    localGraphics2D.drawString("Time Ran: " + str, 182, 390);
    localGraphics2D.drawString("Status: " + getState().toString(), 182, 400);
  }
}